(function() {
	var defaultOptions = {
		tbodyId : 'tbodyId',
		heads : [],
		rst : {},
		sup : {
			id : 'sup',
			template : '共 [index] 条'
		},
		handler : [],
		trGender : defaultTrGender
	};
	var options = null;

	function dataTable(customerOptions) {

		options = $.extend(true, {}, defaultOptions, customerOptions);
		var $tbody = $("#" + options.tbodyId);
		$tbody.empty();
		var cols = options.heads.length;
		var rows = options.rst.result.data.length;

		var handlers = [];
		//初始化为默认的处理
		for (var k = 0; k < cols; k++) {
			handlers[k] = defaultHandler;
		}

		//使用自定义处理覆盖
		for (var k = 0; k < options.handler.length; k++) {
			var col = options.handler[k]["col"];
			var handler = options.handler[k]["f"];
			handlers[Number(col)] = handler;
		}

		for (var i = 0; i < rows; i++) {
			var tr = options.trGender(i);
			var $tr = $(tr);
			for (var j = 0; j < cols; j++) {

				var heads = [];
				if ( typeof options.heads[j] === 'object') {
					heads = options.heads[j];
				} else {
					heads.push(options.heads[j]);
				}

				var arguments = [];
				for (var k = 0; k < heads.length; k++) {
					var value = getHtmlString(getHeadIndexValue(i, options.rst, heads[k]));
					arguments.push(value);
				}
				var td = handlers[j](arguments, i);
				var $td = $("<td>" + td + "</td>");
				$tr.append($td);
			}
			$tbody.append($tr);
		}

		if (customerOptions.sup) {
			var supId = options.sup.id;
			var template = options.sup.template.replace("[index]", options.rst.result.len);
			$("#" + supId).text(template);
		}
	}

	// 默认的处理器，简单的打印第一个参数
	function defaultHandler(args, row) {
		return args[0];
	}

	function defaultTrGender(row) {
		return "<tr></tr>";
	}


	window.dataTable = dataTable;

})(window, jQuery);
